package cn.iocoder.yudao.module.security.controller.admin.whitelistevent;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.security.controller.admin.whitelistevent.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.whitelistevent.WhitelistEventDO;
import cn.iocoder.yudao.module.security.service.whitelistevent.WhitelistEventService;

@Tag(name = "管理后台 - 白名单事件")
@RestController
@RequestMapping("/security/whitelist-event")
@Validated
public class WhitelistEventController {

    @Resource
    private WhitelistEventService whitelistEventService;

    @PostMapping("/create")
    @Operation(summary = "创建白名单事件")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:create')")
    public CommonResult<Long> createWhitelistEvent(@Valid @RequestBody WhitelistEventSaveReqVO createReqVO) {
        return success(whitelistEventService.createWhitelistEvent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新白名单事件")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:update')")
    public CommonResult<Boolean> updateWhitelistEvent(@Valid @RequestBody WhitelistEventSaveReqVO updateReqVO) {
        whitelistEventService.updateWhitelistEvent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除白名单事件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:delete')")
    public CommonResult<Boolean> deleteWhitelistEvent(@RequestParam("id") Long id) {
        whitelistEventService.deleteWhitelistEvent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得白名单事件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:query')")
    public CommonResult<WhitelistEventRespVO> getWhitelistEvent(@RequestParam("id") Long id) {
        WhitelistEventDO whitelistEvent = whitelistEventService.getWhitelistEvent(id);
        return success(BeanUtils.toBean(whitelistEvent, WhitelistEventRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得白名单事件分页")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:query')")
    public CommonResult<PageResult<WhitelistEventRespVO>> getWhitelistEventPage(@Valid WhitelistEventPageReqVO pageReqVO) {
        PageResult<WhitelistEventDO> pageResult = whitelistEventService.getWhitelistEventPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WhitelistEventRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出白名单事件 Excel")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:export')")
    @OperateLog(type = EXPORT)
    public void exportWhitelistEventExcel(@Valid WhitelistEventPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(10000);
        // 查询10页
        List<WhitelistEventDO> all = new ArrayList<>();
        for (int i = 1; i < 11; i++) {
            pageReqVO.setPageNo(i);
            List<WhitelistEventDO> list = whitelistEventService.getWhitelistEventPage(pageReqVO).getList();
            if (CollectionUtil.isNotEmpty(list)) {
                all.addAll(list);
            }
            if (list.size() < 10000) {
                break;
            }
        }
        // 导出 Excel
        ExcelUtils.write(response, "白名单事件.xls", "数据", WhitelistEventExcelVO.class,
                BeanUtils.toBean(all, WhitelistEventExcelVO.class));
    }

}